package com.mybatis;

import com.mybatis.bean.User;
import com.mybatis.io.Resources;
import com.mybatis.mapper.UserMapper;
import com.mybatis.sqlsession.SqlSession;
import com.mybatis.sqlsession.SqlSessionFactory;
import com.mybatis.sqlsession.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

/**
 * 创建人: yyn
 * 创建时间:2021/7/9 23:17
 */
public class MybatisTest {
    private UserMapper userMapper;

    @Before
    public void before() throws Exception {
        InputStream resourcesAsStream = Resources.getResourcesAsStream("Mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourcesAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        userMapper = sqlSession.getMapper(UserMapper.class);
    }

    @Test
    public void test1() throws Exception {
        List<User> userList = userMapper.selectList();
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void test2() {
        User user = new User();
        user.setId(8);
        user.setUsername("zhangsan");
        user.setPassword("123456");
        user.setBirthday("2020-01-01");
        Integer insert = userMapper.insert(user);
        System.out.println(insert);
    }
    @Test
    public void test3(){
        User user = new User();
        user.setId(8);
        user.setUsername("lisi");
        user.setPassword("admin");
        user.setBirthday("2020-01-01");
        Integer update = userMapper.update(user);
        System.out.println(update);
    }


    @Test
    public void test4(){
        Integer delete = userMapper.delete(8);
        System.out.println(delete);
    }
}
